/*
* File: configFile.c
* Version: 1.0
* Last modified on Mon Mars 10 2014 by group 5
* Members:
* Johan Forsblom, Johanna Carlsson, Dragana Todorovic, 
* Hampus Herbertsson, Robin Henriksson
* -----------------------------------------------------
* This file implements the configFile.h interface.
*/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "configFile.h"


/* Static varibles */
static configFileT configFile;


/* Function: loadConfigFile
* usage: loadConfigFile()
* ----------------------------------------------------------------
* This function loads the configFile from disk. If it could not load the file
* default values are set. 
*/
void loadConfigFile(void)
{
	FILE *file;

	file = fopen("serverConfig.ini", "a+");
	if(file == NULL) {
		sprintf(configFile.homePageFolder, "/");
		sprintf(configFile.logFileFolder, "/");

		printf("Could not open: serverConfig.ini\n");
		printf("Using default values:\n");
		printf("Serverport: 80\n");
		printf("Telenetport: 23\n");

		sprintf(configFile.port, "80");
		sprintf(configFile.telNetPort, "23");
	}
	else {
		fscanf(file,"ServerPort:%s\n", configFile.port);
		fscanf(file,"TelnetPort:%s\n", configFile.telNetPort);
		fscanf(file,"HomepageFolder:%s\n", configFile.homePageFolder);
		fscanf(file,"LogfileFolder:%s\n", configFile.logFileFolder);
		fscanf(file,"TelnetPassword:%s\n", configFile.telNetPassWord);
	}
}


/* Function: getServerPort
* usage: getServerPort(serverPort)
* ----------------------------------------------------------------
* This function gets the serverport. 
*/
void getServerPort(char *serverPort)
{
	sprintf(serverPort, "%s",configFile.port);
}


/* Function: getTelNetPort
* usage: getTelNetPort(telnetPort)
* ----------------------------------------------------------------
* This function gets the telnetPort. 
*/
void getTelNetPort(char *telNetPort)
{
	sprintf(telNetPort, "%s",configFile.telNetPort);
}


/* Function: printConfigFile
* usage: printConfigFile()
* ----------------------------------------------------------------
* This function prints the configfile data to the server window.
*/
void printConfigFile(void)
{
	printf("ServerPort:%s\n", configFile.port);
	printf("TelnetPort:%s\n", configFile.telNetPort);
	printf("HomepageFolder:%s\n", configFile.homePageFolder);
	printf("LogfileFolder:%s\n", configFile.logFileFolder);
	printf("TelnetPassword:%s\n", configFile.telNetPassWord);
}


/* Function: getHomePageFolder
* usage: getHomePageFolder(homePageFolder)
* ----------------------------------------------------------------
* This function gets the homePageFolder.
*/
void getHomePageFolder(char *homePageFolder)
{
	sprintf(homePageFolder, "%s", configFile.homePageFolder);
}


/* Function: getLogFileFolder
* usage: getLogFileFolder(logFilefFolder)
* ----------------------------------------------------------------
* This function gets the logFileFolder.
*/
void getLogFileFolder(char *logFileFolder)
{
	sprintf(logFileFolder, "%s", configFile.logFileFolder);
}
